package com.web.servlet;

import com.domain.Student;
import com.service.impl.loginServiceimpl;
import com.service.loginService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String action = request.getParameter("action");
        if ("login".equals(action)) {
            try {
                login(request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    private void login(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException, IOException, ServletException {
        //判断验证码是否正确
        //获取用户输入的验证码
        String verify = request.getParameter("verify");
        //从session域中获取正确的验证码
        String verifyCode = (String) request.getSession().getAttribute("verifyCode");
        //先滤空
        //说明没有填验证码
        if (verify == null) {
            //返回验证码失效的信息
            String message = "账号或密码错误";
            request.setAttribute("message", message);
            //请求转发到登录页面
            request.getRequestDispatcher("login.jsp").forward(request, response);
            return;
        }
        //获取了正确的验证码后，即时清空session中的验证码,为了下次比较
        request.getSession().removeAttribute("verifyCode");
        //判断验证码是否正确
        if (!verify.equals(verifyCode)) {
            //验证码错误,返回登录页面
            String message = "验证码错误";
            request.setAttribute("message", message);
            //请求转发到登录页面
            request.getRequestDispatcher("login.jsp").forward(request, response);
            return;
        }
        //获取表单提交的数据
        Map<String, String[]> parameterMap = request.getParameterMap();
        Student student = new Student();
        BeanUtils.populate(student, parameterMap);
        //调用service的方法判断数据库里是否有这个账号密码
        loginService loginService = new loginServiceimpl();
        Student login = loginService.login(student);
        //判断能否登录成功
        if (login != null) {
            System.out.println("登录成功");
            String good = "登录成功";
            request.getSession().setAttribute("good", good);
            //跳转到查看页面
            //当匹配到用户的登录信息后,就要把信息放到session中,从此进入到登录模式
            request.getSession().setAttribute("login", login);
            response.sendRedirect("UserServlet?action=selectAll");
            return;
        } else {
            System.out.println("登录失败,重新登录");
            String no = "账号或密码错误";
            request.setAttribute("no", no);
           //请求转发到登录页面
            request.getRequestDispatcher("login.jsp").forward(request,response);

        }

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
